 //
//  DFMobileVC.m
//  DFMessage
//
//  Created by lanou on 15/10/28.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "DFMobileVC.h"
#import "DFMobileModel.h"
#import "Header.pch"
#import "PKRequestManager.h"
#import "DFMobileCell.h"
#import "DFClickAppleViewController.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
@interface DFMobileVC ()

@property(nonatomic,strong)NSMutableArray *dataSourceArray;
@property(nonatomic,strong)DFMobileModel *model;

@property(nonatomic,assign)int page;
@end

@implementation DFMobileVC

-(void)viewDidLoad{
    [super viewDidLoad];
    
      _dataSourceArray = [NSMutableArray array];
    [self.tableView registerClass:[DFMobileCell class] forCellReuseIdentifier:@"cell"];
    
    self.tableView.rowHeight = 78;
  
    [self requestDataWithType:0];
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestDataWithType:0];
    }];
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self requestDataWithType:1];
    }];
    
}


-(void)requestDataWithType:(NSInteger)type
{
    if (type == 0) {
        _page = 1;
    }
    else
    {
        _page ++;
    }
    NSString *str = [NSString stringWithFormat:@"http://lib.wap.zol.com.cn/ipj/docList/?v=4.0&class_id=74&isReviewing=NO&last_time=2015-10-30%2016%3A19&page=%d&retina=1&vs=iph440",_page];
       [PKRequestManager requestWithType:GET urlString:str parDic:nil finish:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers |NSJSONReadingMutableLeaves error:nil];
           if (type == 0) {
               [self.dataSourceArray removeAllObjects];
           }
        NSArray *listArray = dic[@"list"];

        for (NSDictionary *modelDic in listArray) {
            _model = [[DFMobileModel alloc]init];
            
            [_model setValuesForKeysWithDictionary:modelDic];
         
            [self.dataSourceArray addObject:_model];
          
        }
           [self.tableView.header endRefreshing];
           [self.tableView.footer endRefreshing];
        [self.tableView reloadData];
    } error:^(NSError *error) {
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        
    }];
}






-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
    return _dataSourceArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DFMobileCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.ConfigureModel = _dataSourceArray[indexPath.row];
    DFMobileModel *model = _dataSourceArray[indexPath.row];
    if (model.picArray.count != 0) {
        [cell.imgsrcImage sd_setImageWithURL:[NSURL URLWithString:model.picArray[0]]];
    }
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DFMobileModel *model = _dataSourceArray[indexPath.row];
    DFClickAppleViewController *mobileWeb = [[DFClickAppleViewController alloc] init];
    mobileWeb.str = model.url;
    [self.navigationController pushViewController:mobileWeb animated:YES];
}

@end
